package com.kun.web.controller.activiti;

import com.kun.dto.ProcessDefinitionDto;
import com.kun.service.service.IProcessDefinitionService;
import com.kun.utils.AjaxResult;
import com.kun.vo.DeploymentVo;
import com.kun.vo.ProcessDefinitionVo;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@Validated
@RestController
@RequestMapping("/process-definition")
public class ProcessDefinitionController {

    private final RepositoryService repositoryService;
    private final IProcessDefinitionService processDefinitionService;

    public ProcessDefinitionController(RepositoryService repositoryService, IProcessDefinitionService processDefinitionService) {
        this.repositoryService = repositoryService;
        this.processDefinitionService = processDefinitionService;
    }

    /**
     * 添加流程定义--在线绘制
     */
    @PostMapping("/online-deployment")
    public ResponseEntity<AjaxResult> onlineDeployment(@Valid @RequestBody ProcessDefinitionDto processDefinitionDto) {
        return AjaxResult.success(processDefinitionService.saveDefinition(processDefinitionDto.getXml(),
                processDefinitionDto.getName()));
    }

    /**
     * 添加流程定义--上传文件
     */
    @PostMapping("/upload")
    public ResponseEntity<AjaxResult> upload(@NotNull(message = "文件不能为空") @Size(min = 1, max = 5, message = "文件最多上传五个")
                                             List<MultipartFile> files) {
        return AjaxResult.success(processDefinitionService.uploadBpmn(files));
    }

    /**
     * 获取流程定义列表
     */
    @GetMapping("/list")
    public ResponseEntity<AjaxResult> list(ProcessDefinitionDto processDefinitionDto) {
        return AjaxResult.success(processDefinitionService.selectDefinitionList(processDefinitionDto));
    }

    /**
     * 获取流程定义XML
     */
    @GetMapping("/xml")
    public ResponseEntity<AjaxResult> xml(@Valid ProcessDefinitionVo processDefinition) {
        return AjaxResult.success(processDefinitionService.getXml(processDefinition.getDeploymentId(),
                processDefinition.getResourceName()));
    }

    /**
     * 获取流程定义部署列表
     */
    @GetMapping("/deployments")
    public ResponseEntity<AjaxResult> deployments() {
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
        List<DeploymentVo> list1 = list.stream().map(deployment ->
                new DeploymentVo(deployment.getId(), deployment.getName(), deployment.getDeploymentTime())).toList();
        return AjaxResult.success(list1);
    }

    /**
     * 删除流程定义
     */
    @DeleteMapping("/{processDefinitionId}")
    public ResponseEntity<AjaxResult> delete(@PathVariable @NotBlank(message = "流程定义ID不能为空") String processDefinitionId) {
        return AjaxResult.success(processDefinitionService.deleteDefinitionById(processDefinitionId));
    }

    /**
     * 查询单个流程定义
     */
    @GetMapping("/{definitionId}")
    public ResponseEntity<AjaxResult> getByDefinitionId(@NotBlank(message = "流程定义ID不能为空")
                                                        @PathVariable String definitionId) {
        return AjaxResult.success(processDefinitionService.selectByDefinitionId(definitionId));
    }

}
